<?php
abstract class AbstractValidator 
{
	protected $config;
	
	public function __construct(Configable $config)
	{
		if (!is_object($config)) // potrzebne?
			throw new Exception('Unable too load config file.');
			
		$this->setConfig($config);
	}
	public function setConfig(Configable $config) 
	{
		$this->config = $config;
	}
	
	public function getConfig()
	{
		return $this->config;
	}
}

class AccountValidator extends AbstractValidator 
{
	public function validateName($name)
	{
		$name = trim($name);
		$lenght = strlen($name);

		if ($lenght < $this->config->get('accountminlenght'))
			throw new Exception('Account name is too short. <br />It should be in the range of '.$this->config->get('accountminlenght').' too '.$this->config->get('accountmaxlenght').' characters.');

		if ($lenght > $this->config->get('accountmaxlenght'))
			throw new Exception('Account name is too long. <br />It should be in the range of '.$this->config->get('accountminlenght').' too '.$this->config->get('accountmaxlenght').' characters.');

		if (preg_match($this->config->get('accountchars'), $name))
			return true;
		
		throw new Exception('In account name you used illegal characters.');
	}
	
	public function validatePassword($password)
	{
		$password = trim($password);
		$lenght = strlen($password);
		
		if ($lenght < $this->config->get('passwordminlenght'))
			throw new Exception('Password is too short. <br />It should be in the range of '.$this->config->get('passwordminlenght').' too '.$this->config->get('passwordmaxlenght').' characters.');
			
		if ($lenght > $this->config->get('passwordmaxlenght'))
			throw new Exception('Password is too long. <br />It should be in the range of '.$this->config->get('passwordminlenght').' too '.$this->config->get('passwordmaxlenght').' characters.');
		
		if(preg_match($this->config->get('passwordchars'), $password))
			return true;
			
		throw new Exception('In password you used illegal characters.');
	}
	
	public function validateEmail($email)
	{
		if(preg_match($this->config->get('emailchars'), $email))
			return true;
			
		throw new Exception('Invalid email format.');
	}
}

class PlayerValidator extends AbstractValidator
{
	public function validateName($name)
	{
		$name = trim($name);
		$lenght = strlen($name);
		
		$min = (int) $this->config->get('characterminlenght');
		$max = (int) $this->config->get('charactermaxlenght');
		
		if ($lenght < $min)
			throw new Exception('Player name is too short. <br />It should be in the range of '.$min.' to '.$max.' characters.');
			
		if ($lenght > $this->config->get('charactermaxlenght'))
			throw new Exception('Player name is too long. <br />It should be in the range of '.$min.' to '.$max.' characters.');
		
		if(preg_match($this->config->get('characterchars'), $name))
			return true;
			
		throw new Exception('In the player name you used illegal characters.');
	}
	
	/*
	sex
	group_id
	*/
}

?>